﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AgriMarketService
{
    public class FollowedAuction
    {
        int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        string userAccName;

        public string UserAccName
        {
            get { return userAccName; }
            set { userAccName = value; }
        }

        int auctionId;

        public int AuctionId
        {
            get { return auctionId; }
            set { auctionId = value; }
        }

        string topic;

        public string Topic
        {
          get { return topic; }
          set { topic = value; }
        }
        string productImage;


        public string ProductImage
        {
            get { return productImage; }
            set { productImage = value; }
        }
        public static List<FollowedAuction> GetFollowedAuctionsByUserAccName(string userAccName) {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            return (from tempFollowedAuction in db.Tbl_Followed_Auctions
                    join tempAuction in db.Tbl_Auctions on tempFollowedAuction.AuctionId equals tempAuction.AuctionId
                    where tempFollowedAuction.UseAccName == userAccName
                    select new FollowedAuction()
                    {
                        id = tempFollowedAuction.Id,
                        userAccName = tempFollowedAuction.UseAccName,
                        AuctionId = tempFollowedAuction.AuctionId,
                        topic = tempAuction.Topic,
                        productImage=tempAuction.ProductImage
                    }
                        ).ToList();
        }
        public static int AddNew(FollowedAuction newFA) {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_Followed_Auction tbl_FA = newFA.ToTbl_FollowedAuction();
            Tbl_Followed_Auction tempTbl_FA=(from temp in db.Tbl_Followed_Auctions 
                                             where temp.AuctionId==newFA.AuctionId && temp.UseAccName==newFA.userAccName
                                                 select temp).FirstOrDefault();
            if (tempTbl_FA != null) {
                return -2;//existed
            }
            db.Tbl_Followed_Auctions.InsertOnSubmit(tbl_FA);
            try
            {
                db.SubmitChanges();
                newFA.id = tbl_FA.Id;
                return newFA.id;
            }
            catch (Exception e)
            {
                return -1;//error
            }
        }
        public static FollowedAuction GetAFollowedAuctionById(int followedAuctionId) {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            FollowedAuction followedAuction = (from temp in db.Tbl_Followed_Auctions
                                               join tempAuction in db.Tbl_Auctions on temp.AuctionId equals tempAuction.AuctionId
                                               where temp.Id == followedAuctionId
                                               orderby temp.Id descending
                                               select new FollowedAuction()
                                                {
                                                    id = temp.Id,
                                                    userAccName = temp.UseAccName,
                                                    AuctionId = temp.AuctionId,
                                                    topic = tempAuction.Topic,
                                                    productImage = tempAuction.ProductImage
                                                }).FirstOrDefault();
            return followedAuction;
        }
        public static int Delete(int deletedFAId){
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_Followed_Auction tempTbl_FA=(from temp in db.Tbl_Followed_Auctions
                                             where temp.Id==deletedFAId
                                             orderby temp.Id descending
                                             select temp).FirstOrDefault();
            if(tempTbl_FA==null)
                return -1; //no exist
            db.Tbl_Followed_Auctions.DeleteOnSubmit(tempTbl_FA);
            try
            {
                db.SubmitChanges();
                return 0;
            }
            catch
            {
                return 1;//error
            }
        }
        public static bool DeleteFollowedAuctionsByAuctionId(int auctionId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            var followedAuctions = (from temp in db.Tbl_Followed_Auctions
                                    where temp.AuctionId == auctionId
                                    select temp
                                      );
            db.Tbl_Followed_Auctions.DeleteAllOnSubmit(followedAuctions);
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;//error
            }
        }
        public Tbl_Followed_Auction ToTbl_FollowedAuction() {
            return new Tbl_Followed_Auction()
            {
                AuctionId=this.auctionId,
                UseAccName=this.userAccName
            };
        }
    }
}